package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.AliyunVodApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.code.ErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuyunyun
 * @data 2021/10/12 23:14
 */
@RestController
@Slf4j
public class AliyunVodController implements AliyunVodApi {
//    aliyun.region = cn-shanghai
//    aliyun.accessKeyId = LTAI5tL8fTe8uQBYoCJcPNF1
//    aliyun.accessKeySecret = swoRu5i908B2goxX09XPesBMgVNmbw

    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @PostMapping("media/vod-token")
    public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest request) {
        String fileTitle = request.getTitle();
        String fileName = request.getFileName();
        try {
//        发送请求客户端
            DefaultAcsClient defaultAcsClient = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
//        获取视频上传地址和凭证响应数据
            CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(defaultAcsClient, fileTitle, fileName);
            VodUploadToken vodUploadToken = new VodUploadToken();
            vodUploadToken.setVideoId(response.getVideoId());
            vodUploadToken.setUploadAddress(response.getUploadAddress());
            vodUploadToken.setRequestId(response.getRequestId());
            vodUploadToken.setUploadAuth(response.getUploadAuth());
            return vodUploadToken;
        } catch (Exception e) {
//            e.printStackTrace();
            log.error("获取上传图片token失败:{}",e.getMessage());
            ExceptionCast.cast(MediaErrorCode.E_140011);
            VodUploadToken vodUploadToken = new VodUploadToken();
            return vodUploadToken;
        }
    }

    @Override
    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable String videoId) {
        try {
            //判断关键数据
            ExceptionCast.cast(StringUtil.isBlank(videoId), CommonErrorCode.E_100101);
            //刷新数据
//        获取客户端
            DefaultAcsClient defaultAcsClient = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(defaultAcsClient, videoId);
            VodUploadToken vodUploadToken = new VodUploadToken();
            vodUploadToken.setVideoId(response.getVideoId());
            vodUploadToken.setUploadAddress(response.getUploadAddress());
            vodUploadToken.setRequestId(response.getRequestId());
            vodUploadToken.setUploadAuth(response.getUploadAuth());
            return vodUploadToken;
        } catch (Exception e) {
            log.error("获取上传图片token失败:{}",e.getMessage());
            ExceptionCast.cast(MediaErrorCode.E_140011);
            VodUploadToken vodUploadToken = new VodUploadToken();
            return vodUploadToken;
        }
    }
}
